package demo;

import application.BinaryTree;
import test.util.ToCharacterArray;

import java.util.Arrays;

public class DemoBinaryTree {
    public static void testRebuild(Character[] order1, Character[] order2, BinaryTree.BinaryTreeRebuildType rebuild_type) {
        BinaryTree binaryTree = new BinaryTree();
        switch (rebuild_type) {
            case PRE_IN:
                System.out.println(String.format("==> 使用 preorder 和 inorder 重构二叉树 <==\n"));
                break;
            case POST_IN:
                System.out.println(String.format("==> 使用 postorder 和 inorder 重构二叉树 <==\n"));
                break;
        }
        binaryTree.rebuild(order1, order2, rebuild_type);
        System.out.println("preorder traversal: " + Arrays.toString(binaryTree.traversal("preorder").toArray()));
        System.out.println("inorder traversal: " + Arrays.toString(binaryTree.traversal("inorder").toArray()));
        System.out.println("postorder traversal: " + Arrays.toString(binaryTree.traversal("postorder").toArray()));
        System.out.println();
        System.out.println("preorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.PREORDER));
        System.out.println("inorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.INORDER));
        System.out.println("postorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.POSTORDER));
        System.out.println();
        System.out.println("二叉树深度: " + binaryTree.getDepth());
        System.out.println("二叉树节点个数: " + binaryTree.countNode());
        System.out.println("二叉树叶子节点个数: " + binaryTree.countLeafNode());
    }
    public static void testSerializeAndDeserialize(Character[] sequence, BinaryTree.BinaryTreeSerializeType serialize_type) {
        BinaryTree binaryTree = new BinaryTree();
        switch (serialize_type) {
            case PREORDER:
                System.out.println(String.format("==> 使用 preorder 和 inorder 反序列化二叉树 <==\n"));
                break;
            case POSTORDER:
                System.out.println(String.format("==> 使用 postorder 和 inorder 反序列化二叉树 <==\n"));
                break;
        }
        binaryTree.deserialize(sequence, serialize_type);
        System.out.println("preorder traversal: " + Arrays.toString(binaryTree.traversal("preorder").toArray()));
        System.out.println("inorder traversal: " + Arrays.toString(binaryTree.traversal("inorder").toArray()));
        System.out.println("postorder traversal: " + Arrays.toString(binaryTree.traversal("postorder").toArray()));
        System.out.println();
        System.out.println("preorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.PREORDER));
        System.out.println("inorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.INORDER));
        System.out.println("postorder serialize: " + binaryTree.serialize(BinaryTree.BinaryTreeSerializeType.POSTORDER));
        System.out.println();
    }
    public static void main(String[] args) {
//        BinaryTree binaryTree = new BinaryTree();
//        Character[] preorder = ToCharacterArray.stringTransform("ABDCEF");
//        Character[] inorder = ToCharacterArray.stringTransform("DBAECF");
//        binaryTree.rebuild(preorder, inorder, "pre&in");
//        System.out.println(Arrays.toString(binaryTree.traversal("postorder").toArray()));
//        System.out.println(binaryTree.serialize("in"));
//
//        Character[] postorder = ToCharacterArray.stringTransform("DBEFCA");
//        Character[] inorder1 = ToCharacterArray.stringTransform("DBAECF");
//        binaryTree.rebuild(postorder, inorder1, "post&in");
//        System.out.println(Arrays.toString(binaryTree.traversal("preorder").toArray()));
//        System.out.println(binaryTree.serialize("post"));
//
//        binaryTree.deserialize( ToCharacterArray.stringTransform("#,#,#,4,2,#,#,3,1"), "post" );
//        System.out.println(Arrays.toString(binaryTree.traversal("preorder").toArray()));
//        System.out.println(binaryTree.getDepth());
//        System.out.println(binaryTree.countLeafNode());
//        System.out.println(binaryTree.countNode());

        Character[] preorder = ToCharacterArray.stringTransform("ABDCEF");
        Character[] inorder = ToCharacterArray.stringTransform("DBAECF");
        testRebuild(preorder, inorder, BinaryTree.BinaryTreeRebuildType.PRE_IN);

        System.out.println("\n\n");
        testSerializeAndDeserialize(ToCharacterArray.stringTransform("#,#,#,4,2,#,#,3,1"), BinaryTree.BinaryTreeSerializeType.POSTORDER);
    }
}
